import com.pbking.component.ComponentBase;
import com.pbking.drawing.DrawShape;
import com.pbking.util.ButterTween;
import com.pbking.util.eventHandling.Event;
import mx.transitions.easing.Elastic;

/**
 * Component window (usable in Flash 7)
 * @author Jason Crist
 */
class com.pbking.component.ui.ComponentWindowLite extends ComponentBase
{
	// VARIABLES //////////
	
	private var modal:Boolean = true;
	private var autoCenter:Boolean = true;
	private var bounceIn:Boolean = true;
	
	private var blockerAlpha:Number = 0;
	private var blocker:MovieClip;
	private var windowAnimation:ButterTween;
	
	// CONSTRUCTION //////////
	
	function ComponentWindowLite()
	{
		super();
	}
	
	private function init():Void
	{
		if(modal)
		{
			this.blocker = this._parent.createEmptyMovieClip(this._name + "_blocker", this._parent.getNextHighestDepth());
			DrawShape.box(this.blocker, 0, 0, 10, 10, 0, blockerAlpha);
			blocker.onRelease = function(){};
			blocker.useHandCursor = false;
			this.swapDepths(blocker);
		}
		
		if(autoCenter)
		{
			center();
		}
		
		if(bounceIn)
		{
			bounceInAnimation();
		}
	}
	
	//override to deal with the block clip as well
	public function setVisible(newVal:Boolean):Void
	{
		super.setVisible(newVal);
		
		blocker._visible = this._visible;
	}

	// PUBLIC FUNCTIONS //////////
	
	public function close():Void
	{
		this.blocker.removeMovieClip();
		dispatch(new Event("close", this));
		this.removeMovieClip();
	}

	/**
	* center the window in it's parent MovieClip (optionally use the stage even if the stage isn't the parent)
	*/
	public function center(forceStageRef:Boolean){
		//centers the window on the screen

		if(this._parent == _root || forceStageRef){

			var point:Object = new Object();
			point.x = ((Stage.width-this.width)/2);
			point.y = ((Stage.height-this.height)/2);

			this._parent.globalToLocal(point);

			this._x = point.x;
			this._y = point.y;

		} else {
			this._x = ((this._parent._width-this.width)/2);
			this._y = ((this._parent._height-this.height)/2);
		}

		if(this.blocker)
		{
			var point:Object = {x:0, y:0};
			this.blocker.globalToLocal(point);
			blocker._x += point.x;
			blocker._y += point.y;
			blocker._width = Stage.width;
			blocker._height = Stage.height;
		}
	}
	
	private function bounceInAnimation():Void
	{
		this._alpha = 0;
		
		windowAnimation = new ButterTween(Elastic.easeOut, 800, 30);
		windowAnimation.addProperty(this, "_alpha", 50, 100);
		windowAnimation.addProperty(this, "_xscale", 50, 100);
		windowAnimation.addProperty(this, "_yscale", 50, 100);
		windowAnimation.addProperty(this, "_x", this._x + this._width*.25, this._x);
		windowAnimation.addProperty(this, "_y", this._y + this._height*.25, this._y);
		windowAnimation.start();
	}
}